﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Strom.Core.Characters;

namespace Strom.Client.GUI
{
    public partial class FormCharactersSelection : Form
    {
        private readonly List<Character> _characters;

        public FormCharactersSelection(List<Character> characters)
        {
            InitializeComponent();
            ClientManager.GUI.CharactersSelection = this;
            _characters = characters;
            ClientManager.Client.Client.CurrentAccount.Characters = _characters;
        }

        private void FormCharactersSelection_Load(object sender, EventArgs e)
        {
            foreach (var current in _characters)
            {
                listBox1.Items.Add(current.Name + "\tNiveau: " + current.Stats.Level);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            ClientManager.GUI.Login.Close();
            base.OnClosed(e);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!btn_select.Enabled)
                btn_select.Enabled = true;
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            if (ClientManager.GUI.CreateCharacter == null)
                new FormCreateCharacter().ShowDialog();
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            var item = (string)listBox1.SelectedItem;
            string name = item.Split('\t')[0];
            ClientManager.Client.Client.CurrentCharacter = ClientManager.Client.Client.CurrentAccount.Characters.First(current => current.Name == name);
            Hide();
            new FormGame().Show();
        }

        public void AddNewCharacter(string name)
        {
            _characters.Add(new Character(name,new CharacterStats(1, 50, 50),new CharacterGraphics(0), ClientManager.Client.Client.CurrentAccount.Infos.Login));
            listBox1.Items.Add(name + "\tNiveau: " + 1);
            listBox1.Update();
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = listBox1.IndexFromPoint(e.Location);
            if(index != ListBox.NoMatches)
            {
                btn_select_Click(null,null);
            }
        }
    }
}
